QueueType

Queue

Queue<'a> hands values from producer fibers to one logical consumer, in FIFO order, with an overflow strategy chosen when it is created. This module creates queues and offers to them; take values, inspect, and shut a queue down with the Dequeue functions, which accept a Queue.

Create a queue with make and a QueueStrategy, or with a shorthand: bounded suspends producers while full, dropping discards the new value, sliding evicts the oldest, and unbounded never refuses. makeScoped shuts the queue down when the current scope closes.

Interrupting a suspended offer never enqueues its value, and interrupting a suspended take never loses one. When zero or many consumers must each see every value, use a Hub instead.

tryOffer returns immediately and can be called from a synchronous callback. Its QueueTryOfferResult distinguishes acceptance, a full back-pressure queue, discard, eviction, and shutdown. A Full or Shutdown result leaves the offered value outside the queue.

Read the Queue guide for strategies, batching, shutdown, and draining on stop.

Specification

Kind
Type
Members
9
Examples
0
makeSignature
Queue.make strategy
makeScopedSignature
Queue.makeScoped strategy
boundedSignature
Queue.bounded capacity
droppingSignature
Queue.dropping capacity
slidingSignature
Queue.sliding capacity

Summary

NameSignatureSynopsis
Operations
makeQueue.make strategyCreates a queue with the given overflow strategy.
makeScopedQueue.makeScoped strategyCreates a queue that is shut down when the current scope closes.
boundedQueue.bounded capacityCreates a lossless queue whose offer suspends while it holds capacity values.
droppingQueue.dropping capacityCreates a queue whose offer discards the new value and returns false when full.
slidingQueue.sliding capacityCreates a queue whose offer evicts the oldest value when full and returns true.
unboundedQueue.unbounded ()Creates a queue that never rejects or suspends an offer.
offerQueue.offer value queueAdds a value according to the queue's strategy.
tryOfferQueue.tryOffer value queueTries to add a value immediately, without starting a Flow or suspending the caller.
offerAllQueue.offerAll values queueAdds values in order according to the queue's strategy.

Operations

kind:member

make

Queue.make strategy
Member
Creates a queue with the given overflow strategy.

Parameters

NameTypeDescription
strategyQueueStrategy

Returns

Flow<'env, 'error, Queue<'a>>

Verification Examples

Queue.make (QueueStrategy.Sliding 1)
kind:member

makeScoped

Queue.makeScoped strategy
Member
Creates a queue that is shut down when the current scope closes.

Parameters

NameTypeDescription
strategyQueueStrategy

Returns

Flow<'env, 'error, Queue<'a>>

Verification Examples

flow {
    let! (jobs: Queue<string>) = Queue.makeScoped (QueueStrategy.BackPressure 64)
    do! jobs |> Queue.offer "first job" |> Flow.ignore
}
|> Flow.scoped
kind:member

bounded

Queue.bounded capacity
Member
Creates a lossless queue whose offer suspends while it holds capacity values.

Parameters

NameTypeDescription
capacityint

Returns

Flow<'env, 'error, Queue<'a>>

Verification Examples

Queue.bounded 64
kind:member

dropping

Queue.dropping capacity
Member
Creates a queue whose offer discards the new value and returns false when full.

Parameters

NameTypeDescription
capacityint

Returns

Flow<'env, 'error, Queue<'a>>
kind:member

sliding

Queue.sliding capacity
Member
Creates a queue whose offer evicts the oldest value when full and returns true.

Parameters

NameTypeDescription
capacityint

Returns

Flow<'env, 'error, Queue<'a>>
kind:member

unbounded

Queue.unbounded ()
Member
Creates a queue that never rejects or suspends an offer.

Parameters

NameTypeDescription
unit

Returns

Flow<'env, 'error, Queue<'a>>
kind:member

offer

Queue.offer value queue
Member
Adds a value according to the queue's strategy.

Parameters

NameTypeDescription
value'a
queueQueue<'a>

Returns

Flow<'env, 'error, bool>

Verification Examples

flow {
    let! (jobs: Queue<string>) = Queue.bounded 8
    return! jobs |> Queue.offer "job"
}
kind:member

tryOffer

Queue.tryOffer value queue
Member
Tries to add a value immediately, without starting a Flow or suspending the caller.

Parameters

NameTypeDescription
value'a
queueQueue<'a>

Returns

QueueTryOfferResult
kind:member

offerAll

Queue.offerAll values queue
Member
Adds values in order according to the queue's strategy.

Parameters

NameTypeDescription
values'a seq
queueQueue<'a>

Returns

Flow<'env, 'error, bool>

Verification Examples

flow {
    let! (samples: Queue<float>) = Queue.unbounded ()
    do! samples |> Queue.offerAll [ 1.0; 2.0; 3.0 ] |> Flow.ignore
    return! samples |> Dequeue.takeUpTo 500
}